import os
import shutil
from easydict import EasyDict
import yaml

def mk_path(path, remove=False):
    try:
        if not os.path.exists(path):
            os.makedirs(path)
        else:
            if remove:
                shutil.rmtree(path, ignore_errors=True)
    except Exception as e:
        print(e)


def loadyaml(file_path):
    if file_path is not None:
        try:
            with open(file_path, "r", encoding="utf-8") as f:
                return EasyDict(yaml.load(f, Loader=yaml.FullLoader))
        except IOError as e:
            print(e)
    else:
        print("文件路径为空")
        return None

from torchvision.transforms.functional import normalize
import torch.nn as nn
import numpy as np
import os


def denormalize(tensor, mean, std):
    mean = np.array(mean)
    std = np.array(std)

    _mean = -mean / std
    _std = 1 / std
    return normalize(tensor, _mean, _std)


class Denormalize(object):
    def __init__(self, mean, std):
        mean = np.array(mean)
        std = np.array(std)
        self._mean = -mean / std
        self._std = 1 / std

    def __call__(self, tensor):
        if isinstance(tensor, np.ndarray):
            return (tensor - self._mean.reshape(-1, 1, 1)) / self._std.reshape(-1, 1, 1)
        return normalize(tensor, self._mean, self._std)

# if __name__ == '__main__':
#     # 明确指定日志输出的文件路径和日志级别
#     logger = _get_logger('./logs/test.log', 'info')
#     logger.info('日志输出测试')
